package com.lq.schoolshop.util;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Author
 * @Date2021/2/5 17:18
 * @Version V1.0
 *  Rabbit工具类
 **/
public class RabbitMQUtils {
    private static ConnectionFactory connectionFactory;
    //类加载执行之前执行一次
    static{

        connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("39.105.74.232");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/schoolshop");
        connectionFactory.setUsername("shopadmin");
        connectionFactory.setPassword("shopadmin");
    }

    public static Connection getConnection(){
        try{
            return connectionFactory.newConnection();
        }catch(Exception e){
           e.printStackTrace();
        }
        return null;
    }

    public static void closeConnectionAndChanel(Channel channel,Connection conn){
       try {
           if(channel!=null){
               channel.abort();
           }
           if(conn!=null){
               conn.close();
           }
       }catch (IOException e){
          e.printStackTrace();
       }catch (AlreadyClosedException e){
           e.printStackTrace();
       }
    }


}
